//
//  ProductSizeCell.m
//  MJApp
//
//  Created by John on 15/3/30.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "ProductSizeCell.h"
#define  kSizeBtnW 50
#define  kSizeBtnH 20

@interface ProductSizeCell()
@property (weak, nonatomic) IBOutlet UILabel *sizeLable;
@property (nonatomic,strong)UIButton *lastBtn;

@end
@implementation ProductSizeCell

- (void)awakeFromNib {
    // Initialization code
//    [self UI];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)UIWithSizeArr:(NSArray *)sizeArr{

    int MaxNum = IOS_IPHONE_WINDOW_WIDE/(kSizeBtnW+12);
    CGFloat startY = CGRectGetMaxY(self.sizeLable.frame);
    for (int i = 0; i < sizeArr.count; ++i) {
        
        
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(i%MaxNum*(kSizeBtnW+12)+12,i/MaxNum*(kSizeBtnH+10)+10+startY, kSizeBtnW, kSizeBtnH)];
        btn.tag = i;
        [btn addTarget:self action:@selector(choiseProductSize:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font =[UIFont systemFontOfSize:13];
        [btn setTitleShadowColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitle:[NSString stringWithFormat:@"%@寸",sizeArr[i][@"size"]] forState:UIControlStateNormal];
        btn.layer.cornerRadius = 3;
        btn.layer.borderWidth = 0.5;
        btn.layer.borderColor = [UIColor grayColor].CGColor;
        [self.contentView addSubview:btn];
        
    }
    
    
}
+ (instancetype)cellForTableview:(UITableView *)tableview{

    static NSString * ID = @"ProductSizeCell";
    ProductSizeCell * cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ProductSizeCell" owner:nil options:nil].lastObject;
    }
    int MaxNum = IOS_IPHONE_WINDOW_WIDE/(kSizeBtnW+12);
     CGFloat startY = CGRectGetMaxY(cell.sizeLable.frame);
    cell.cellH = 2/MaxNum*(kSizeBtnH+10)+10+startY+kSizeBtnH + 10;
    
    return cell;
}
- (void)setInfoDic:(NSDictionary *)infoDic{

    [super setInfoDic:infoDic];
    NSArray * sizeArr = self.sizeArr;//infoDic[@"size"];
    [self UIWithSizeArr:sizeArr];
    
}
- (void)setSizeArr:(NSArray *)sizeArr{
    _sizeArr = sizeArr;
    [self UIWithSizeArr:sizeArr];
}

- (void)choiseProductSize:(UIButton *)btn{
    NSArray * subViewArr = [self.contentView subviews];
    for (UIView * view in subViewArr) {
        if ([view isKindOfClass:[UIButton class]]) {
            view.layer.borderColor = [UIColor grayColor].CGColor;
            UIButton * btn = (UIButton *)view;
            btn.selected = NO;
        }
        
    }
    btn.layer.borderColor = [UIColor redColor].CGColor;
    btn.selected = YES;
    if ([self.delegate respondsToSelector:@selector(ProductSizeCell:BtnClick:)]) {
        [self.delegate ProductSizeCell:self BtnClick:btn];
    }
}
@end
